package com.example.arithmeticleetcode.leetcode.July;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

import org.apache.tomcat.util.codec.binary.Base64;

import io.netty.handler.codec.base64.Base64Encoder;

/**
 * @author fangzhen@focusmedia.cn
 * @creation time 2021/7/7
 */
public class TestBase64ToMultipartFile {

    public static void main(String[] args) {
        //请求path
        String host = "https://openapi-retail-public.fmtest.tech";
        String path = "/v1/creative/upload";
        String appKey = "203953068";
        String appSecret = "K9KZDFZPfSycLyrg3IxihmupA5wUcNBo";

        //图片转化为二进制的Base64编码字符串
        String imgFile = "C:\\Users\\Focusmedia\\Pictures\\分众自身宣传片.mp4";
        InputStream in = null;
        byte[] data = null;
        //读取图片字节数组
        try {
            in = new FileInputStream(imgFile);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String base64String = new String(Base64.encodeBase64(data));
        System.out.println(base64String);
        System.out.println("============");
        System.out.println(base64String.getBytes(StandardCharsets.UTF_8).length / 1024/1024);
        System.out.println(base64String.getBytes().length);
        //Body内容
        String body = "{\"inputs\":[{\"image\":{\"dataType\":50,\"dataValue\":\"" + base64String
                + "\"},\"configure\":{\"dataType\":50,\"dataValue\":\"{\\\"side\\\":\\\"face\\\"}\"}}]}";

        Map<String, String> headers = new HashMap<>();
        //（必填）根据期望的Response内容类型设置
        headers.put("Accept", "application/json");
        //（可选）Body MD5,服务端会校验Body内容是否被篡改，//（可选）Body MD5，服务端会校验Body内容是否被篡改，建议Body非Form表单时添加此Header建议Body非Form表单时添加此Header
//        headers.put(HttpHeader.HTTP_HEADER_CONTENT_MD5, MessageDigestUtil.base64AndMD5(body));
        //（POST/PUT请求必选）请求Body内容格式
        headers.put("Content-Type", "application/json; charset=UTF-8");

//        Request request = new Request(Method.POST_STRING, host, path, appKey, appSecret, Constants.DEFAULT_TIMEOUT);
//        request.setHeaders(headers);
//        request.setSignHeaderPrefixList(CUSTOM_HEADERS_TO_SIGN_PREFIX);
//
//        request.setStringBody(body);
        /**
         * 重要提示如下：
         * 代码中用到的类请下载 https://github.com/aliyun/api-gateway-demo-sign-java
         *
         * 具体调用请参考https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/test/java/com/aliyun/api/gateway/demo/Demo.java
         **/
        //调用服务端
//        Response response = Client.execute(request);
//
//        System.out.println(JSON.toJSONString(response));
    }
}
